<?php
include_once ("../../php/include_all.php");
$tahun=date('Y');
session_start();

logs('Melihat usulan di SKPD ');
?>
<div class="box span12 geo-box">
    <div class="box-header well">
        <H2>Usulan SKPD</H2>
    </div>
    <table>
        <tr>
            <td><label class="control-label" for="nama-urusan">Nama Urusan</label></td>
            <td></td>
            <td><select name="id_tahun" >
                    <option>pilih tahun</option>
                </select></td>
        </tr>
    </table>
    <div class="box-content">
        <div class="accordion" id="accordion2">
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="btn btn-primary" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">Tambah Data</a>
                </div>
                <div id="collapseTwo" class="accordion-body collapse">
                    <div class="accordion-inner" >
					<form id="form_usSKPD" class="form-horizontal" name="masterSKPD" method="post" action="">
						<input type="hidden" name="act" value="simpan_usSKPD">
						<input type="hidden" name="id_usSKPD" id="id_usSKPD" value="0">
						<table class="table-form">
                                <tr>
                                    <td>Program dan Kegiatan</td>
                                    <td>:</td>
                                    <td colspan="5"> 
                                        <a href="#windowProKeg" data-toggle="modal" id="program-kegiatan" name="program-kegiatan" onclick="detailprogKeg()">Pilih program dan Kegiatan</a> (<span id="textProgram"></span>)
                                        <input type="hidden" id="idprogram" name="idprogram" value="" />
                                        <input type="hidden" id="idkegiatan" name="idkegiatan" value="" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Indikator Hasil Program</td>
                                    <td>:</td>
                                    <td colspan="5"><textarea  class="input-xxlarge" rows="2" id="ind_hslpro" name="ind_hslpro"></textarea></td>
                                </tr>
                                <tr>
                                    <td>Indikator Keluaran</td>
                                    <td>:</td>
                                    <td colspan="5"><textarea  class="input-xxlarge" rows="2" id="ind_klr" name="ind_klr"></textarea></td>
                                </tr>
                                <tr>
                                    <td>Indikator Hasil Kegiatan</td>
                                    <td>:</td>
                                    <td colspan="5"><textarea  class="input-xxlarge" rows="2" id="ind_hslkeg" name="ind_hslkeg"></textarea></td>
                                </tr>
                                <tr>
                                    <td>Target Hasil</td>
                                    <td>:</td>
                                    <td colspan="5"><input type="text" class="span3" id="target_hsl" name="target_hsl"></input></td>
                                </tr>
								<input type="hidden" id="tahun" name="tahun" value=<? echo "$tahun"; ?>>
						</table>
						<div id="kotak">
							<div id="cobadt"></div>
							<div id="input0"></div>
						</div>
						<a href="javascript:tambah_rincian();">Tambah Rincian Kegiatan</a>
                        <table>        
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td colspan="5">
                                        <a class="btn btn-primary bt-simpan-usSKPD"><div id="loader" style="margin-top:2px"></div>&nbsp;Simpan</a>
                                     
                                    </td>
                                </tr>
						</table>
					</form>
                    </div>
                </div>
                <div id="windowProKeg" class="modal hide fade" style="width:800px" valign="midle" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <header class="modal-header">
                        <a href="#" class="close" data-dismiss="modal">x</a>
                        <h3>Program dan Kegiatan</h3>
                    </header>
                    <div class="modal-body" id="modal_progKeg">
                    </div>
                </div>
				 <div id="showDetail" class="modal hide fade" style="width:1000px" valign="midle" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <header class="modal-header">
                        <a href="#" class="close" data-dismiss="modal">x</a>
                        <h3>Detail Kegiatan</h3>
                    </header>
                    <div class="modal-body" id="modal_detailKeg">
                    </div>
               	</div>
            </div>
        </div>
        <table class="table table-striped table-bordered geo-table" >
            <thead>
                <tr>
                    <th rowspan="2" style="text-align:center;" width="5%;">Kode</th>
                    <th rowspan="2" style="text-align:center;" width="30%;">Program/Kegiatan</th>
                    <th colspan="4" style="text-align:center;" width="35%;">Pagu Indikatif</th>
                    <th rowspan="2" style="text-align:center; "width="20%;"> </th>
                </tr>
				<tr>
					<th style="text-align:center;">APBD Kab</th>
					<th style="text-align:center;">APBD Prov</th>
					<th style="text-align:center;">APBN</th>
					<th style="text-align:center;">Total</th>
				</tr>
            </thead>
            <tbody id="tbody_usulanSKPD">
                <?php
             //   $no = 1;
                $query = get_datas("select distinct S.id_skpd, P.kode_program, P.id_program, nama_program, nama_skpd
from mus_usulan_skpd U, mus_program P, mus_kegiatan K, mus_skpd S 
where S.id_skpd=U.id_skpd and U.id_kegiatan=K.id_keg and K.id_program=P.id_program and U.id_skpd=".musrenbang_idskpd."
order by P.id_program  
");
                //tampil data
                foreach ($query as $query) {
                    ?>
                    <tr><td style="text-align:center;"><?php echo $query['kode_program'] ?></td>
                        <td colspan="6"><b><?php echo $query['nama_program']  ?></b></td>
					</tr>
                    <?php
          //          $no++;
			//		$kuku = get_datas("select * from mus_skpd S, mus_usulan_skpd U where S.id_skpd=U.id_skpd");
					$keg= get_datas("select distinct id_kegiatan, U.id_uskpd, K.kode_kegiatan, S.nama_skpd, K.nama_kegiatan,
(select sum(ur.apbdprov) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)as tot_apbdprov,
(select sum(ur.apbdkab) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)as tot_apbdkab,
(select sum(ur.apbn) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)as tot_apbn,
((select sum(ur.apbdprov) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)+(select sum(ur.apbdkab) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)+(select sum(ur.apbn) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)) as tot   
from mus_rinc_usulan_skpd R,mus_usulan_skpd U, mus_program P, mus_kegiatan K, mus_skpd S where S.id_skpd=U.id_skpd and U.id_kegiatan=K.id_keg and K.id_program=P.id_program and U.id_uskpd=R.id_uskpd and K.id_program=".$query['id_program']." and U.id_skpd=".musrenbang_idskpd." ");
					foreach ($keg as $keg){
						?>
						<tr><td><?php echo $keg['kode_kegiatan']?></td>
							<td><a href="#showDetail" data-toggle="modal" id="kegiatan" name="kegiatan" onclick="detaiKeg(<?php echo $keg['id_uskpd'] ?>)"><?php echo $keg['nama_kegiatan']?></a></td>
							<td><?php echo $keg['tot_apbdkab']?></td>
							<td><?php echo $keg['tot_apbdprov']?></td>
							<td><?php echo $keg['tot_apbn']?></td>
							<td><?php echo $keg['tot_apbn']?></td>
							<td style="text-align:center;">
                            <a href = "#collapseTwo" data-toggle="collapse" class="btn btn-small btn-info edit" name="<?php echo $keg['id_uskpd']; ?>"><i class="icon-edit icon-white"></i>&nbsp; Ubah</a>
                            <a class="btn btn-small btn-danger hapus" onclick="tesHapus(this);" name="<?php echo $keg['id_uskpd'];?>" ><i class="icon-trash icon-white"></i>&nbsp; Hapus</a></td>
             
						</tr>
						<?
					}
                }
                ?>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript">

function detailprogKeg () {
	var source = "SKPD/usulan_SKPD/proKeg.php";
	var progkeg = $.get(source);
	progkeg.done(function(dataprokeg){
		$('#modal_progKeg').html(dataprokeg);
	});
}

function detaiKeg(k){
	 var source = "SKPD/usulan_SKPD/detail_keg.php?id_uskpd="+k;
	 var kegiatan = $.get(source);
	 kegiatan.done(function(datas){
		$('#modal_detailKeg').html(datas);
	 });
}

function getData(a) {
    var radio = $('#optionsRadiosPro_' + a).val();
    sources = "SKPD/usulan_SKPD/tools.php?cde=pro&idpro=" + radio;
	var list_keg = $('.item_prog_' + radio).children(".list_keg");
    var keg_open = $(".keg_open");
    keg_open.slideUp();
    keg_open.removeClass('keg_open');
    if (!list_keg.hasClass("keg_listed")) {
        var keg = $.get(sources);
		list_keg.addClass('loading');
        list_keg.slideDown();
        keg.done(function(datas) {
            setTimeout(function(){
            list_keg.slideUp();
            list_keg.removeClass('loading')
            setTimeout(function(){
                list_keg.html(datas);
				list_keg.addClass("keg_listed");
                list_keg.slideDown();
                list_keg.addClass("keg_open");                                                                
            },500);
        },2000);                                                        
    })
    }else {
        list_keg.addClass('keg_open');
        list_keg.slideDown();
    }
	$('#idprogram').val(radio);
 }
 
function onclickKegiatan(a,pro){
    var text = $('#textProgram');
    $('#idkegiatan').val(a);
    var src = "SKPD/usulan_SKPD/tools.php?cde=text&pro="+pro+"&kgt="+a;
    text.removeClass().addClass('loading');
    var get = $.get(src);
    get.done(function(data){
       text.removeClass('loading');
       text.html(data);
    });
}

counter = 0;
function tambah_rincian(){
	var ccc = $('#kotak').find("input[name='rows[]']");
	var xcounter = ccc.length+1;
	counterNext = counter + 1 ;
	document.getElementById("input"+counter).innerHTML="<table align='center'><tr><td><input type='hidden' name='id[]'><input type='text' style='width:150px;' name='usulan[]' placeholder='Usulan'></td><td><input type='text' style='width:120px;' name='lokasi[]' placeholder='Lokasi'></td><td><input type='text' style='width:80px;' name='volume[]' placeholder='Volume'></td><td><input type='text' style='width:100px;' name='apbdkab[]' placeholder='APBD Kab'></td><td><input type='text' style='width:100px;' name='apbdprov[]' placeholder='APBD Prov'></td><td><input type='text' style='width:100px;' name='apbn[]' placeholder='APBN'></td><td><input type='text' name='prioritas[]'  value="+xcounter+" style='width:50px'></td><input type='hidden' name='rows[]'  value='"+xcounter+"'></tr></table><div id=\"input"+counterNext+"\"></div>";	
	counter++;
}

var urls='SKPD/usulan_SKPD/aksi_usSKPD.php';
$('.bt-simpan-usSKPD').click(function(){
	var btn = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	load.addClass('spinner pull-left');
	btn.removeClass('btn-primary').addClass('btn-info');
	
	var form = $('#form_usSKPD');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	
	post.done(function(res){
		var result = res.split('__');
		if(result.length==3){
			if(result[0]=='success'){
				rslt.html('<font color="green">Tersimpan</font>');
				setTimeout(function() {
					rslt.html('');
				},1500);
				
			var tbody= $('#tbody_usulanSKPD');
			//alert(result[2]);
			tbody.html(result[2]);
			init();
			document.getElementById('id_usSKPD').value='';
		//	document.getElementById('id_bid').selected=true;
			document.getElementById('skpd').value='';
			document.getElementById('idprogram').value='';
			document.getElementById('idkegiatan').value='';
			document.getElementById('ind_hslpro').value='';
			document.getElementById('ind_klr').value='';
			document.getElementById('ind_hslkeg').value='';
			document.getElementById('target_hsl').value='';
			document.getElementById('tahun').value='';
			$('#row').val(0);
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
		}else{
			rslt.html('<font color="red">'+res+'</font>');
		}
		load.removeClass();
		btn.removeClass('btn-info').addClass('btn-primary');
		var col =	$('#collapseTwo');
		col.removeClass('in');
		col.css('height','0');
		$('#idprogram').val('');
		$('#idkegiatan').val('');
		$('#textProgram').html('');
		var ktk = $('#kotak');
		ktk.html("<div id='cobadt'></div><div id='input0'></div>");
	});
});

function tesHapus(a){
	console.log(a);
}

function init(){
	$('.hapus').click(function(){
		var btn = $(this);
		var pid = this.name;
		var load = $(this);
		console.log(btn+' == '+pid);
		load.html('<div class="spinner"></div>');
		bootbox.confirm("Anda akan menghapus data ini?", function(result){
			if(result==true){
				if(pid!=0){
					var post= $.post(urls, {act:"hapus_uSKPD", id_usSKPD:pid});
					post.done(function(res){
						var hasil = res.split('__');
						if(hasil.length == 2){
							if(hasil[0]== 'success'){
								var tbody = $('#tbody_usulanSKPD');
								tbody.html(hasil[1]);
								
								init();
							}
						}
					});
					load.html('<i class="icon-trash icon-white"></i>');
				}
			}else {
				load.html('<i class="icon-trash icon-white"></i>');
			}
		});
	});
	$('.edit').click(function(){
		var id_usSKPD = this.name;
		var form = $('#form_usSKPD');
		var post = $.post(urls, {act:"edit_uSKPD", id_usSKPD:id_usSKPD});
		post.done(function(res){
		console.log(res);
				var value = res.split('__');
				$('#id_usSKPD').val(value[0]);
				form.find('select[name="bidang"]').val(value[4])
				form.find('select[name="skpd"]').val(value[1]);
				form.find('select[name="skpd"]').html(value[11])
			//	form.find('select[name="textProgram"]').val(value[3]);
			//	form.find('select[name="textProgram"]').val(value[4]);
				form.find('textarea[name="ind_hslpro"]').val(value[7]);
				form.find('textarea[name="ind_klr"]').val(value[8]);
				form.find('textarea[name="ind_hslkeg"]').val(value[6]);
				form.find('input[name="target_hsl"]').val(value[9]);
				$('#cobadt').html(value[12]);
				$('#idprogram').val(value[3]);
				$('#idkegiatan').val(value[4]);
				$('#textProgram').html(value[13]);
				$('#row').val(0);
		});
	});
}
init();
</script>